package gui.mvc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import client.RmiClient;

import api.Address;
import api.Person;
import api.Phone;

/**
 * @author Ann Raboschuk
 */
public class Controller
{
   private List<Person> people;

   @SuppressWarnings("unchecked")
   public Controller()
   {
      people = RmiClient.loadAll();
//      people = new ArrayList<Person>();
//      Set<Phone> phones = new HashSet<Phone>();
//      phones.add(new Phone("909203"));
//      phones.add(new Phone("08080"));
//      Set<Address> addresses = new HashSet<Address>();
//      addresses.add(new Address("Ukraine", "Kiev", "Khreschtik", "45a", "17"));
//      addresses.add(new Address("USA", "Miami", "Bay road", "112", null));
//      Person person = new Person("Jhons", "Ann", 22, 'F', "fa@mail.com", phones, addresses);
//      person.setId(1L);
//      people.add(person);
//      
//      phones = new HashSet<Phone>();
//      phones.add(new Phone("3523636"));
//      phones.add(new Phone("080280"));
//      addresses = new HashSet<Address>();    
//      addresses.add(new Address("USA", "Miami", "Bay road", "112", null));
//      person = new Person("Jhons", "Ann", 22, 'F', "fa@mail.com", phones, addresses);
//      person.setId(2L);
//      people.add(person);
   }

   public List<Person> getPeople()
   {
      return people;
   }
   
   public void setPeople(List<Person> persons)
   {
      people.clear();
      for (Person person:persons)
      {
         people.add(person);
//         RmiClient.updatePerson(person);
      }
   }

   public void deletePerson(int index)
   {
//      RmiClient.deletePerson(people.get(index));
      people.remove(index);
   }

   public void editPerson(int index, String[] fields)
   {
      if (fields != null)
      {
         Person changedPerson = people.get(index);
         changedPerson.setSurname(fields[0]);
         changedPerson.setName(fields[1]);
         changedPerson.setAge(Integer.parseInt(fields[2]));
         changedPerson.setSex(fields[3].charAt(0));
         changedPerson.setEmail(fields[4]);
//         changedPerson.setPhone(fields[5]);
//         changedPerson.setAddress(fields[6]);
         
//         RmiClient.updatePerson(changedPerson);
      }
   }

   public void addPerson(String[] fields)
   {
      if (fields != null)
      {
         Person person = new Person();
//            new Person(fields[0], fields[1], Integer
//               .parseInt(fields[2]), fields[3].charAt(0), fields[4], fields[5],
//               fields[6]);
         people.add(person);
//         Long id=RmiClient.addPerson(person);
//         person.setId(id);
      }
   }
}